package com.txsj.scan.presenter;

import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.bean.BaseListResult;
import com.txsj.scan.bean.CompanyBean;
import com.txsj.scan.http.ApiServer;
import com.txsj.scan.http.requestframework.RetrofitManager;
import com.txsj.scan.utils.MD5Util;
import com.txsj.scan.utils.PreferenceUtil;
import com.txsj.scan.view.ISelectCompanyView;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @Created by WangChuangJun on 2019/2/20
 * @Describe
 */
public class SelectCompanyPresenter extends BasePresenter<ISelectCompanyView> {
    public SelectCompanyPresenter(ISelectCompanyView view) {
        super(view);
    }

    @Override
    protected void initModel() {

    }

    public void getCompanyList() {
        Map<String, String> params = new HashMap<>();
//        StringBuilder builder = new StringBuilder();
//        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.BRAND_ID, ""));
//        builder.append("|");
//        builder.append(PreferenceUtil.getInstance().getInt(PreferenceUtil.USER_ID, 0));
//        builder.append("|");
//        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.TOKEN, ""));
//        builder.append("|");
//        builder.append(0);
//        builder.append("|");
//        params.put("brandtoken", builder.toString());
//        params.put("login_brand", PreferenceUtil.getInstance().getString(PreferenceUtil.BRAND_ID,
//                ""));
//        params.put("login_cbo", PreferenceUtil.getInstance().getInt(PreferenceUtil.USER_ID,
//                0) + "");
        params.put("mobile",PreferenceUtil.getInstance().getString(PreferenceUtil.MOBILE,""));
        params.put("timeStamp", System.currentTimeMillis() / 1000 + "");//加时间戳参数
        params.put("sign", MD5Util.md5(params));//md5加密并加参数
        view.onLoading();
        RetrofitManager.getDefault().create(ApiServer.class).getCompanyList(RetrofitManager
                .getHeader(), params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseListResult<CompanyBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseListResult<CompanyBean> result) {
                        if (result.isSuccess()){
                            if (null != result.getData() && result.getData().size() > 0){
                                view.onSuccess(result.getData());
                            }else {
                                view.onEmpty();
                            }
                        }else {
                            view.onError(result.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.onError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
